﻿namespace com.vs.DataAccess
{
    using com.vs.General;
    using System;
    using System.Collections;
    using System.Data;

    public class DataHelperParameterCache
    {
        private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());

        private DataHelperParameterCache()
        {
        }

        public static void CacheParameterSet(Session session, string commandText, params IDataParameter[] commandParameters)
        {
            paramCache[commandText] = commandParameters;
        }

        private static IDataParameter[] CloneParameters(IDataParameter[] originalParameters)
        {
            IDataParameter[] parameterArray = new IDataParameter[originalParameters.Length];
            int index = 0;
            int length = originalParameters.Length;
            while (index < length)
            {
                parameterArray[index] = (IDataParameter) ((ICloneable) originalParameters[index]).Clone();
                index++;
            }
            return parameterArray;
        }

        private static IDataParameter[] DiscoverSpParameterSet(Session session, string spName, bool includeReturnValueParameter)
        {
            IDataParameter[] parameterArray2;
            using (DataHelper helper = new DataHelper(session))
            {
                IDbConnection connection = helper.DBFactory.CreateConnection(session.ConnectionString);
                IDbCommand cmd = helper.DBFactory.CreateCommand(spName, connection);
                try
                {
                    cmd.Connection.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    helper.DBFactory.DeriveParameters(cmd);
                    if (!includeReturnValueParameter)
                    {
                        helper.DBFactory.RemoveReturnValueParameters(cmd.Parameters);
                    }
                    IDataParameter[] array = new IDataParameter[cmd.Parameters.Count];
                    cmd.Parameters.CopyTo(array, 0);
                    parameterArray2 = array;
                }
                finally
                {
                    cmd.Dispose();
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                        connection.Dispose();
                    }
                }
            }
            return parameterArray2;
        }

        public static IDataParameter[] GetCachedParameterSet(Session session, string commandText)
        {
            IDataParameter[] originalParameters = (IDataParameter[]) paramCache[commandText];
            if (originalParameters == null)
            {
                return null;
            }
            return CloneParameters(originalParameters);
        }

        public static IDataParameter[] GetSpParameterSet(Session session, string spName)
        {
            using (DataHelper helper = new DataHelper(session))
            {
                if (helper.DBFactory is SqlFactory)
                {
                    return GetSpParameterSet(session, spName, false);
                }
                return GetSpParameterSet(session, spName, true);
            }
        }

        public static IDataParameter[] GetSpParameterSet(Session session, string spName, bool includeReturnValueParameter)
        {
            string str = spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");
            IDataParameter[] originalParameters = (IDataParameter[]) paramCache[str];
            if (originalParameters == null)
            {
                object obj2;
                paramCache[str] = obj2 = DiscoverSpParameterSet(session, spName, includeReturnValueParameter);
                originalParameters = (IDataParameter[]) obj2;
            }
            return CloneParameters(originalParameters);
        }
    }
}

